<template>
  <div>
    <el-generate-form
      ref="generateFormRef"
      v-model="formData"
      :data="widgetForm"
    >
    </el-generate-form>
    <el-button type="primary" @click="handleSubmit">提交</el-button>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import widgetForm from './widgetForm'
import { request } from '../../../request'

const generateFormRef = ref()
const formData = ref()

// 修改options
async function queryOptions() {
  const res = await request('/api/iot/options')
  const optionsData = res.body
  if (res.body instanceof Array) {
    generateFormRef.value.setOptions('select_9c0ac5bf33ab47bfb2b5024757d78a78', optionsData.map(item => ({
      label: item.cnName,
      value: item.id
    })))
  }
}
queryOptions()

const handleSubmit = () => {
  generateFormRef.value
    .getData()
    .then((data) => {
      console.log(data, '表单数据')
    })
    .catch((err) => {
      console.log(err)
    })
}
</script>
